package com.sys.sysserver.service.usercrud;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sys.sysserver.domain.bean.PageBean;
import com.sys.sysserver.domain.dto.UserQueryDTO;
import com.sys.sysserver.domain.po.User;
import com.sys.sysserver.mapper.UserCrudMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserCrudServiceImpl implements UserCrudService {
    private final UserCrudMapper userCrudMapper;

    @Override
    public PageBean page(Integer pageNum, Integer pageSize, UserQueryDTO userQueryDTO) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> userList = userCrudMapper.page(userQueryDTO);
        Page<User> p = (Page<User>) userList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    @Override
    public User getById(Integer id) {
        User user = userCrudMapper.getById(id);
        return user;
    }

    @Override
    public void edit(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userCrudMapper.edit(user);
    }

    @Override
    public void del(List<Integer> ids) {
        userCrudMapper.del(ids);
    }

    @Override
    public void add(User user) {
        String password = user.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(password);
        user.setDel(0);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userCrudMapper.add(user);
    }
}
